<template>
  <div class="list">
    <!--1. 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--2. 卡片组件 -->
    <el-card class="box-card">
      <!-- 3.表格 组件
       el-table 表格分区 
       el-table-column表格列 
       -->
      <el-table :data="postList" stripe border="" style="width: 100%">
        <!-- id号 -->
        <el-table-column prop="id" label="" width="50"> </el-table-column>
        <!-- 封面 -->
        <el-table-column label="封面" width="180">
          <template slot-scope="scope">
            <img
              class="cover"
              v-if="scope.row.cover.length"
              :src="scope.row.cover[0].url | imgUrl"
            />
          </template>
        </el-table-column>
        <!-- prop展示只有文本 -->
        <!-- 标题 -->
        <el-table-column
          prop="title"
          label="标题"
          width="300"
        ></el-table-column>

        <el-table-column label="类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1">
              <i class="el-icon-tickets"></i>
              <span>文章</span>
            </span>
            <span v-else>
              <i class="el-icon-video-camera-solid"></i>
              <span>视频</span>
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="user.nickname" label="作者名称" width="180">
        </el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            {{ scope.row.id }}
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--4.分页器组件-->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[4, 6, 8, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getPostAPI } from "@/apis/news";
export default {
  data() {
    return {
      postList: [],
      //当前选择页码
      pageIndex: 1,
      pageSize: 4,
      total: 0,
    };
  },
  async created() {
    //获取文章数据1 用于获取总条数的
    const res = await getPostAPI({ pageSize: 400 });
    console.log(res);
    //获取到总条数
    this.total = res.data.data.length;
    //根据初始页码和页容量第一次获取数据
    this.getPostList();
  },
  methods: {
    async getPostList() {
      //获取文章数据2  用于展示分页展示
      const res2 = await getPostAPI({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      this.postList = res2.data.data;
    },
    handleSizeChange(val) {
      // console.log("页容量", val);
      this.pageIndex = 1;
      this.pageSize = val;
      this.getPostList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getPostList();
    },
  },
};
</script>
 
<style lang="less" scoped>
.box-card {
  margin-top: 15px;
}
.cover {
  width: 50px;
  height: 50px;
}
.block {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>